import 'package:common_utils/common_utils.dart' show TimerUtil, LogUtil;
import 'package:flutter/material.dart';
import 'package:testlzhu/utils/text_su_back.dart';

class TextAa extends StatefulWidget {
  int totalTime = 10;

//圆角度数
  double radius;

  final double height;
  final double width;

  final double borderWidth;
  final Color borderColor;
  final Color backcolor;
  final TextStyle? textStyle;

  TextBack? textBack;

  TextAa(this.totalTime, this.radius,
      {Key? key, this.height = 30, this.width = 70, this.borderWidth = 1, this.borderColor = Colors.red, this.backcolor = Colors.white, this.textStyle, this.textBack})
      : super(key: key);

  @override
  _TextAaState createState() => _TextAaState();
}

class _TextAaState extends State<TextAa> {
  int _count = 0;
  late TimerUtil _timerUtil;

  @override
  void initState() {
    super.initState();
    _timerUtil = TimerUtil(mTotalTime: widget.totalTime * 1000);
    _timerUtil.setOnTimerTickCallback((int tick) {
      double _tick = tick / 1000;
      setState(() {
        _count = _tick.toInt();
      });

      LogUtil.e(_count);
      if (_tick == 0) {
        if (widget.textBack != null) {
          widget.textBack!.onSuc();
        }
      }
    });
    _timerUtil.startCountDown();
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      child: Container(
        margin: const EdgeInsets.only(left: 40, top: 40),
        alignment: Alignment.center,
        height: widget.height,
        width: widget.width,
        decoration: BoxDecoration(
            //背景颜色
            color: widget.backcolor,
            //四周圆角
            borderRadius: BorderRadius.all(Radius.circular(widget.radius)),
            //四周边框
            border: Border.all(width: widget.borderWidth, color: widget.borderColor)),
        child: Text("$_count s 跳过", style: (widget.textStyle == null) ? const TextStyle(fontSize: 12) : widget.textStyle),
      ),
      onTap: () {
        if (widget.textBack != null) {
          widget.textBack!.onSuc();
        }
      },
    );
  }

  @override
  void dispose() {
    super.dispose();
    _timerUtil.cancel();
//记得在dispose里面把timer cancel。
  }
}
